package com.evaluationsystem.system.serivce.impl;

import cn.hutool.core.date.DateUtil;
import com.evaluationsystem.Utils.ModularNameUtils;
import com.evaluationsystem.Utils.UserContextUtils;
import com.evaluationsystem.system.dao.CustomerAssessmentDao;
import com.evaluationsystem.system.dao.DictionaryDao;
import com.evaluationsystem.system.entity.Tlogs;
import com.evaluationsystem.system.entity.Tree;
import com.evaluationsystem.system.serivce.CustomerAssessmentService;
import com.evaluationsystem.system.serivce.TlogsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class CustomerAssessmentServiceImpl implements CustomerAssessmentService {

    @Autowired
    CustomerAssessmentDao dao;

    @Autowired
    UserContextUtils userContextUtils;

    @Autowired
    DictionaryDao dictionaryDao;

    @Autowired
    TlogsService logsService;

    @Override
    public Map<String, Object> treeDatas() {
        Map<String,Object> map = new HashMap<>();
        List<Tree> ct2 = dao.selCustomerAssessment();
        List<Tree> pt = new ArrayList<>();
        pt.add(new Tree().setChildren(ct2).setTitle("客户评定组").setParentId("0").setLeaf(false).setLevel("1").setSpread(true));
        map.put("code","200");
        map.put("msg","ok");
        map.put("data",pt);
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> treeDatasAdd(Map<String, Object> param) {
        /*{vcustomername=qw}*/
       Map<String,Object> map = new HashMap<>();
       if (StringUtils.isNotEmpty(dao.selCustomerNameByCustomerName(param))){
           map.put("code","5001");
           map.put("msg","评定组名称已存在");
           return map;
       }
        param.put("vcustomerid",userContextUtils.getMaxid(dao.selCustomerAssessMentMaxID(),"KH",4,"0"));
        param.put("vinsuserid",userContextUtils.getUserId());
        param.put("vinsdate", DateUtil.now());
        try {
            int a = dao.insertCustomerAssessMent(param);
            a = logsService.add(new Tlogs()
                    .setVoperationcontent(param.get("vcustomername").toString())/*操作内容*/
                    .setVmodularname(ModularNameUtils.CustomerAssessmentAdd)/*操作模块名称*/
                    .setVpagename(ModularNameUtils.CustomerAssessmentPageName)/*操作页面名称*/
                    .setVoperation(userContextUtils.getUserId())/*操作人*/
            );
            map.put("code","200");
            map.put("msg","添加成功");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("code","5002");
            map.put("msg","数据接口访问异常，请联系管理员");
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> treeDatasUpdata(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();
        if (StringUtils.isNotEmpty(dao.selCustomerNameByCustomerName(param))){
            map.put("code","5001");
            map.put("msg","评定组名称已存在");
            return map;
        }
        try {
            param.put("vupduserid",userContextUtils.getUserId());
            param.put("vupduate",DateUtil.now());
            int a = dao.updataCustomerAssessMentById(param);
            a = logsService.add(new Tlogs()
                    .setVoperationcontent(param.get("vcustomername").toString())/*操作内容*/
                    .setVmodularname(ModularNameUtils.CustomerAssessmentUpdata)/*操作模块名称*/
                    .setVpagename(ModularNameUtils.CustomerAssessmentPageName)/*操作页面名称*/
                    .setVoperation(userContextUtils.getUserId())/*操作人*/
                    .setVrecordscode(param.get("vcustomerid").toString())
            );
            map.put("code","200");
            map.put("msg","修改成功");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("code","5002");
            map.put("msg","数据接口访问异常，请联系管理员");
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> treeDatasDel(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();
        try {
            int a = dao.delCustomerAssessMentById(param);
            a = dao.delEvaluatmemberByPositionid(param);
            a = logsService.add(new Tlogs()
                    .setVoperationcontent(param.get("vcustomerid").toString())/*操作内容*/
                    .setVmodularname(ModularNameUtils.CustomerAssessmentDel)/*操作模块名称*/
                    .setVpagename(ModularNameUtils.CustomerAssessmentPageName)/*操作页面名称*/
                    .setVoperation(userContextUtils.getUserId())/*操作人*/
                    .setVrecordscode(param.get("vcustomerid").toString())
            );
            map.put("code","200");
            map.put("msg","删除成功");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("code","5002");
            map.put("msg","数据接口访问异常，请联系管理员");
        }
        return map;
    }

    @Override
    public Map<String, Object> evaluatMemberFalse(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();
        System.out.println(param);
        PageHelper.startPage(Integer.parseInt(param.get("page").toString()), Integer.parseInt(param.get("limit").toString()));
        List<Map<String, Object>> falseStaffinfos =  dao.evaluatMemberFalse(param);
        PageInfo<Map<String, Object>> PageInfos = new PageInfo<>(falseStaffinfos);
        map.put("code","0");//解析接口状态
        map.put("msg","success");//解析提示文本
        map.put("count",PageInfos.getTotal());//解析数据长度
        map.put("data",PageInfos.getList());//解析数据列表
        return map;
    }

    @Override
    public Map<String, Object> evaluatMemberTure(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();
        PageHelper.startPage(Integer.parseInt(param.get("page").toString()), Integer.parseInt(param.get("limit").toString()));
        List<Map<String, Object>> Turetaffinfos =  dao.evaluatMemberTure(param);
        PageInfo<Map<String, Object>> PageInfos = new PageInfo<>(Turetaffinfos);
        System.out.println("falseStaffinfos - PageInfos.getList() == "+PageInfos.getList());
        map.put("code","0");//解析接口状态
        map.put("msg","success");//解析提示文本
        map.put("count",PageInfos.getTotal());//解析数据长度
        map.put("data",PageInfos.getList());//解析数据列表
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> evaluatMemberTrueAdd(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();
        String vpositionid = param.get("vpositionid").toString();
        List<Map<String,Object>> FalseArr =(List<Map<String,Object>>)param.get("FalseArr");
        //{vstaffid=112, vteamid=BZ0002, vpositionid=ZW0001, vpositionname=1, vfactoryflg=0, vfactoryflgname=陆友, vteamname=222, vappraiseflgname=否, vstaffname=112, vappraiseflg=0, vphone=13804192302}
        try {
            for (Map<String,Object> staffinfo:FalseArr) {
                Map<String,Object> evaluatmember = new HashMap<>();
                evaluatmember.put("vpositionid",vpositionid);
                evaluatmember.put("vstaffid",staffinfo.get("vstaffid"));
                int a = dao.evaluatmemberAdd(evaluatmember);
                a = logsService.add(new Tlogs()
                        .setVoperationcontent(staffinfo.get("vstaffid").toString())/*操作内容*/
                        .setVmodularname(ModularNameUtils.EvaluatmemberAdd)/*操作模块名称*/
                        .setVpagename(ModularNameUtils.CustomerAssessmentPageName)/*操作页面名称*/
                        .setVoperation(userContextUtils.getUserId())/*操作人*/
                        .setVrecordscode("评定组ID:"+vpositionid)
                );
            }
            map.put("code","200");
            map.put("msg","移入成功");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("code","5002");
            map.put("msg","数据接口访问异常，请联系管理员");
        }
        return map;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Map<String, Object> evaluatMemberTrueDel(Map<String, Object> param) {
        Map<String,Object> map = new HashMap<>();
        String vpositionid = param.get("vpositionid").toString();
        List<Map<String,Object>> TureArr =(List<Map<String,Object>>)param.get("TureArr");
        //{vstaffid=112, vteamid=BZ0002, vpositionid=ZW0001, vpositionname=1, vfactoryflg=0, vfactoryflgname=陆友, vteamname=222, vappraiseflgname=否, vstaffname=112, vappraiseflg=0, vphone=13804192302}
        try {
            for (Map<String,Object> staffinfo:TureArr) {
                Map<String,Object> evaluatmember = new HashMap<>();
                evaluatmember.put("vpositionid",vpositionid);
                evaluatmember.put("vstaffid",staffinfo.get("vstaffid"));
                int a = dao.evaluatmemberDel(evaluatmember);
                a = logsService.add(new Tlogs()
                        .setVoperationcontent(staffinfo.get("vstaffid").toString())/*操作内容*/
                        .setVmodularname(ModularNameUtils.EvaluatmemberDel)/*操作模块名称*/
                        .setVpagename(ModularNameUtils.CustomerAssessmentPageName)/*操作页面名称*/
                        .setVoperation(userContextUtils.getUserId())/*操作人*/
                        .setVrecordscode("评定组ID:"+vpositionid)
                );
            }
            map.put("code","200");
            map.put("msg","移除成功");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            map.put("code","5002");
            map.put("msg","数据接口访问异常，请联系管理员");
        }
        return map;
    }


}
